package com.jtp7.demo.config;

import com.jtp7.demo.entity.response.PageListResult;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;

import java.time.Duration;
import java.util.Objects;

@Configuration
public class MyRedisConfig {

    /**
     * 修改redis默认使用的是jdk的序列化
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer<Object> objectJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        redisTemplate.setDefaultSerializer(objectJackson2JsonRedisSerializer);
        return  redisTemplate;
    }

    @Bean
    public RedisTemplate<String, PageListResult> redisTemplate1(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String, PageListResult> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer<PageListResult> objectJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(PageListResult.class);
        redisTemplate.setDefaultSerializer(objectJackson2JsonRedisSerializer);
        return  redisTemplate;
    }



    @Primary //默认指定的缓存管理器
    @Bean
    public RedisCacheManager toRedisCacheManager(RedisTemplate<String, PageListResult> redisTemplate) {
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                //.entryTtl(Duration.ofHours(1))
                .entryTtl(Duration.ofSeconds(60))

                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()));
        RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
        return redisCacheManager;
    }

    @Bean
    public RedisCacheManager truckinfoRedisCacheManager(RedisTemplate<String, PageListResult> redisTemplate1) {
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(Objects.requireNonNull(redisTemplate1.getConnectionFactory()));
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofHours(1))
               // .entryTtl(Duration.ofSeconds(60))

                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate1.getValueSerializer()));
        RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
        return redisCacheManager;
    }


}